package com.amos.teaching_aids.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;


public abstract class AdapterBase<T> extends BaseAdapter {
    protected final Context mContext;
    private ListView mListview;
    protected List<T> mData;//数据源
    protected final int [] mLayoutResArrays;//子项布局的样式种类数

    public AdapterBase(Context context, int [] layoutResArrays){
        this(context, layoutResArrays, null);
    }

    public AdapterBase(Context context, int [] layoutResArrays, List<T> data){
        this.mData = data == null ? new ArrayList<T>() : data;
        this.mContext = context;
        this.mLayoutResArrays = layoutResArrays;
    }

    public AdapterBase(Context context, int[] layoutResArrays, List<T> data,ListView listView) {
        this.mData = data == null ? new ArrayList<T>() : data;
        this.mContext = context;
        this.mLayoutResArrays = layoutResArrays;
        this.mListview=listView;
    }

    public void setData(ArrayList<T> data){
        this.mData = data;
        this.notifyDataSetChanged();
    }

    public void addData(ArrayList<T> data){
        if(data != null){
            this.mData.addAll(data);
        }
        this.notifyDataSetChanged();
    }

    public void addData(T data) {
        this.mData.add(data);
        this.notifyDataSetChanged();
    }

    public ArrayList<T> getAllData() {
        return (ArrayList<T>)this.mData;
    }

    @Override
    public int getCount() {
        if(this.mData == null){
            return 0;
        }
        return this.mData.size();
    }

    @Override
    public Object getItem(int position) {
        if(position > this.mData.size()){
            return null;
        }
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return this.mLayoutResArrays.length;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolderHelper helper = getAdapterHelper(position, convertView, parent);
        T item = (T)getItem(position);//得到数据源容器的实体类
        convert(helper, item);//调用我们暴露给业务子类的convert交互接口对ListView item进行定制填充。
//        if (convertView!=null&&mListview!=null) {
//            if (mListview.getLastVisiblePosition()==position-1)
//                startItemAnim(convertView);
//        }
        return helper.getView();//返回子项布局
    }
//    private void startItemAnim(View view) {
//        Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.zoom_in);
//        view.startAnimation(animation);
//    }
    protected abstract void convert(ViewHolderHelper helper, T item);
    //给Adapter得到一个ViewHolderHelper对象
    protected ViewHolderHelper getAdapterHelper(int position, View convertView, ViewGroup parent){
        return ViewHolderHelper.get(mContext,convertView,parent,mLayoutResArrays[0] ,position);
     }
}
